from langchain import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.tools import tool, Tool
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field

llm = ChatOpenAI(
    api_key="sk-a3f7718fb81f43b2915f0a6483b6661b",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    model="llama-4-scout-17b-16e-instruct",  # 此处以qwen-plus为例，您可按需更换模型名称。模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
    # other params...
)
@tool
def weather()-> str:
    """今天的天气"""
    return "多云"

print(weather.args)

class Calculator(BaseModel):
    a:int = Field(description="第一个数")
    b:int = Field( description="第二个数")

@tool("my_tool1", description="计算两个数的和",args_schema=Calculator)
def my_tool1(a:int,b:int)-> int:
    print("调用了计算两数和工具")
    """计算两个数的和"""
    return a+b

tool_calu = Tool(name="weather",func=weather,description="今天的天气")
tools = [tool_calu]
llm.bind_tools(tools)
prompt = hub.pull("hwchase17/openai-functions-agent")
print(prompt.messages)
tool_agent = create_tool_calling_agent(llm=llm,tools=tools,prompt=prompt)
agent = AgentExecutor(agent=tool_agent,tools=tools)

print(agent.invoke({"input":"今天的天气"}))